package com.company.OfferGuide;

import java.util.PriorityQueue;

public class 滑动窗口的最大值 {

    public static void main(String[] args) {
        int[] maxSlidingWindow = maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3);
        for (int i = 0; i < maxSlidingWindow.length; i++) {
            System.out.println(maxSlidingWindow[i]);
        }
    }

    public static int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length == 0) return new int[]{};
        if (k > nums.length) return new int[]{};
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1,o2) -> {return o2 - o1;});
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(nums[i]);
        }
        int[] res = new int[nums.length + 1 - k];
        int index = 0;
        for (int i = k; i < nums.length; i++) {
            res[index] = priorityQueue.peek();
            priorityQueue.remove(nums[index]);
            priorityQueue.offer(nums[i]);
            index++;
        }
        res[index] = priorityQueue.peek();

        return res;
    }

}
